﻿using Protocol.Constant;
using Protocol.Dto;
using Protocol.Dto.Fight;
using Script.UI.Msg;
using UnityEngine;
using UnityEngine.UI;

namespace Script.UI._2.Fight
{
    public class StatePanel : UIBase
    {
        // 角色的数据
        protected UserDto userDto;

        private Image imgIdentity;
        private Text txtReady;
        private Image imgChat;
        private Text txtChat;

        // Start is called before the first frame update
        protected virtual void Start()
        {
            // 获取操作对象
            imgIdentity = transform.Find("imgIdentity").GetComponent<Image>();
            imgChat = transform.Find("imgChat").GetComponent<Image>();
            txtReady = transform.Find("txtReady").GetComponent<Text>();
            txtChat = imgChat.transform.Find("Text").GetComponent<Text>();


            // 初始状态
            txtReady.gameObject.SetActive(false);
            imgChat.gameObject.SetActive(false);
        }


        protected virtual void Awake()
        {
            Bind(UIEvent.PLAYER_READY,
                UIEvent.PLAYER_HIDE_STATE,
                UIEvent.PLAYER_LEAVE,
                UIEvent.PLAYER_ENTER,
                UIEvent.PLAYER_CHAT,
                UIEvent.PLAYER_CHANGE_IDENTITY);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case UIEvent.PLAYER_READY:
                {
                    if (userDto == null)
                    {
                        /*Debug.LogError("发生啥了");*/
                        return;
                    }

                    int userId = (int) message;
                    // 如果是自身角色 就显示
                    if (userDto.Id == userId)
                    {
                        ReadyState();
                    }

                    break;
                }
                case UIEvent.PLAYER_HIDE_STATE:
                    txtReady.gameObject.SetActive(false);
                    break;
                case UIEvent.PLAYER_LEAVE:
                {
                    if (userDto == null)
                    {
                        /*Debug.LogError("发生啥了");*/
                        return;
                    }


                    int userId = (int) message;
                    if (userDto.Id == userId)
                    {
                        SetPanelActive(false);
                    }

                    break;
                }
                case UIEvent.PLAYER_ENTER:
                {
                    if (userDto == null)
                    {
                        /*Debug.LogError("发生啥了");*/
                        return;
                    }

                    int userId = (int) message;
                    if (userDto.Id == userId)
                    {
                        SetPanelActive(true);
                    }

                    break;
                }
                case UIEvent.PLAYER_CHAT:
                {
                    if (userDto == null)
                    {
                        /*Debug.LogError("发生啥了");*/
                        return;
                    }

                    if (message is ChatMsg chatMsg && userDto.Id == chatMsg.UserId)
                    {
                        ShowChat(chatMsg.Text);
                    }

                    break;
                }
                case UIEvent.PLAYER_CHANGE_IDENTITY:
                {
                    if (userDto == null)
                    {
                        break;
                    }

                    if (message is GrabDto msg && userDto.Id == msg.userId)
                    {
                        SetIdentity(Identity.LANDLORD);
                    }

                    break;
                }
            }
        }

        protected virtual void ReadyState()
        {
            txtReady.gameObject.SetActive(true);
        }

        /// <summary>
        /// 设置身份
        /// 0 是farmer 1 是地主
        /// </summary>
        private void SetIdentity(int identity)
        {
            string identityStr = identity == 0 ? "Farmer" : "Landlord";
            imgIdentity.sprite = Resources.Load<Sprite>("Identity/" + identityStr);
        }

        /// <summary>
        /// 显示时间
        /// </summary>
        private int showTime = 2;

        /// <summary>
        /// 计时器
        /// </summary>
        private float timer;

        /// <summary>
        /// 是否显示
        /// </summary>
        private bool isShow;


        protected virtual void Update()
        {
            if (isShow)
            {
                timer += Time.deltaTime;

                if (timer >= showTime)
                {
                    SetChatActive(false);
                    timer = 0f;
                    isShow = false;
                }
            }
        }

        private void SetChatActive(bool active)
        {
            imgChat.gameObject.SetActive(active);
        }

        private void ShowChat(string text)
        {
            // 设置文字
            txtChat.text = text;

            // 显示动画
            SetChatActive(true);
            isShow = true;
        }
    }
}